package xiao.ze.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xiao.ze.demo.entity.CourseType;
import xiao.ze.demo.service.impl.CourseTypeServiceImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2021/2/14 14:51
 * Role of:
 * @version: v1.0
 */
@Controller
@RequestMapping("/CourseType")
@SessionAttributes(value = {"user", "page"})//myClick没用
public class CourseTypeController {

    @Resource
    CourseTypeServiceImpl courseTypeService;

    /**
     * 添加课程类型
     *
     * @return
     */
    @PostMapping("/addCourseType")
    public String addCourseType(CourseType courseType, Model model) {
        int num = courseTypeService.addCourseType(courseType);
        if (num <= 0) {
            model.addAttribute("error", "添加失败");
        } else {
            model.addAttribute("error", "添加成功");
        }
        return "/courseType/input_course_type";
    }


    /**
     * 进入新增课程类型
     *
     * @return
     */
    @GetMapping("/input_course_type")
    public String input_course_type() {
        return "/courseType/input_course_type";
    }

    /**
     * 进入课程类型管理
     *
     * @return
     */
    @GetMapping("/list_course_type")
    public String list_course_type(Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "8") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseType> courseTypes = courseTypeService.selAllCourseType();
        PageInfo pageInfo = new PageInfo(courseTypes);
        model.addAttribute("page", pageInfo);
        return "/courseType/list_course_type";
    }


    /**
     * 删除课程类型
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{typeId}")
    public String remove(@PathVariable("typeId") Integer id, HttpSession session) {//,@PathVariable("pageNum")Integer pageNum
        System.out.println("删除" + id);
        courseTypeService.delCourseType(id);

        PageInfo pageInfo = (PageInfo) session.getAttribute("page");
        return "redirect:/CourseType/list_course_type?pageNum=" + pageInfo.getPageNum();
    }

    /**
     * 进入修改课程类型
     *
     * @param id
     * @return
     */
    @GetMapping("/preUpdate/{typeId}")
    public String preUpdate(@PathVariable("typeId") Integer id, Model model) {
        System.out.println("进入修改课程");
        CourseType courseType = courseTypeService.selCourseTypeId(id);
        model.addAttribute("courseType", courseType);
        return "/courseType/update_course_type";
    }

    @PutMapping("/update")
    public String update(CourseType courseType, HttpSession session) {
        System.out.println("修改课程类型");
        courseTypeService.upCourseType(courseType);
        PageInfo pageInfo = (PageInfo) session.getAttribute("page");
        return "redirect:/CourseType/list_course_type?pageNum=" + pageInfo.getPageNum();
    }

}
